package com.shuorikeji.huohuo.ui.mh;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.imageview.ShapeableImageView;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.MhPl1Adapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhDetailInfoBean;
import com.shuorikeji.huohuo.bean.MhDetailInfoMainBean;
import com.shuorikeji.huohuo.bean.MhPlInfoBean;
import com.shuorikeji.huohuo.bean.MhPlMainBean;
import com.shuorikeji.huohuo.bean.UserInfoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.AndroidBug5497Workaround;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.GsonUtil;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhPlActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.siv_fm)
    ShapeableImageView sivFm;
    @BindView(R.id.tv_name)
    TextView tvMhName;
    @BindView(R.id.tv_zz)
    TextView tvZz;
    @BindView(R.id.tv_all_dzs)
    TextView tvAllDzs;
    @BindView(R.id.tv_all_pls)
    TextView tvAllPls;
    @BindView(R.id.tv_pl_sort)
    TextView tvPlSort;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.siv_tx)
    ShapeableImageView sivTx;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_fb_pl)
    TextView tvFbPl;

    private String MH_DETAIL_INFO;
    private MhDetailInfoBean mhDetailInfo;
    private UserInfoBean userInfo;
    private int curPlPage;
    private MhPl1Adapter mhPl1Adapter;
    private List<MhPlInfoBean> mhPlList = new ArrayList<>();
    private int MH_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
        AndroidBug5497Workaround.assistActivity(MhPlActivity.this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_pl);
    }

    @Override
    protected void initData() {
        curPlPage = 1;
        initSmartRefreshLayout();
        initAdapter();
        userInfo = BusinessUtils.getUserInfo();
        initIntent();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(MhPlActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPlPage = 1;
                getMhPlList();
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPlPage = curPlPage + 1;
                getMhPlList();
            }
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        mhPl1Adapter = new MhPl1Adapter(this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:

                        break;
                }
            }
        });
        rvData.setAdapter(mhPl1Adapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        MH_DETAIL_INFO = intent.getStringExtra("MH_DETAIL_INFO");
        mhDetailInfo = GsonUtil.parseJsonToBean(MH_DETAIL_INFO, MhDetailInfoBean.class);
        MH_ID = intent.getIntExtra("MH_ID", -1);
        if (mhDetailInfo != null) {
            showMhInfo();
            getMhPlList();
        } else {
            getMhDetail();
        }
    }

    private void getMhDetail() {
        mPresenter.getMhDetail(true, MH_ID, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhDetailInfoMainBean data = (MhDetailInfoMainBean) object;
                        mhDetailInfo = data.getData();
                        showMhInfo();
                        getMhPlList();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhPlActivity.this, msg);
                }
            }
        });
    }

    private void showMhInfo() {
        BusinessUtils.setLoadImg(MhPlActivity.this, Constant.IMG_OSS_BASE_URL + mhDetailInfo.getCover_image(), sivFm);
        tvMhName.setText(mhDetailInfo.getName());
        tvZz.setText("作者：" + mhDetailInfo.getAuthor());
        if (userInfo != null) {
            BusinessUtils.setLoadImg(MhPlActivity.this, userInfo.getAvatar(), sivTx);
        }
    }

    private void getMhPlList() {
        mPresenter.getMhPlList(true, mhDetailInfo.getId(), 1, curPlPage, 20, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        srlLayout.finishLoadMore();
                        srlLayout.finishRefresh();
                        MhPlMainBean data = (MhPlMainBean) object;
                        if (curPlPage == 1) {
                            mhPlList.clear();
                        }
                        mhPlList.addAll(data.getData().getData());
                        mhPl1Adapter.setData(mhPlList);
                        break;
                    case 2:
                        srlLayout.finishLoadMore();
                        srlLayout.finishRefresh();
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhPlActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void fbPl() {
        mPresenter.addMhPl(true, 10, mhDetailInfo.getId(), etContent.getText().toString(), new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        etContent.setText("");
                        ToastShowUtil.showToastCenter(MhPlActivity.this, "评论成功，等待后台审核");
                        //getMhPlList();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhPlActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        tvPlSort.setOnClickListener(view -> {

        });
        tvFbPl.setOnClickListener(view -> {
            if (BusinessUtils.isEmpty(etContent.getText().toString())) {
                ToastShowUtil.showToastCenter(MhPlActivity.this, "请发表一下你的看法吧～");
            } else {
                if (mhDetailInfo == null) {
                    ToastShowUtil.showToastCenter(MhPlActivity.this, "暂无漫画信息");
                } else {
                    fbPl();
                }
            }
        });
    }
}